home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / RSOUNIT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  3.5 KB  |  119 lines

  1. #include "rsounit.h"
  2.  
  3. Rect::Rect(int X, int Y, int W, int H)
  4. // Initializes the dimensions of an Rso
  5. {
  6.   Xul = X;  Yul = Y;  // Upper left-hand corner
  7.   Xlr = X + W - 1;    // Lower right-hand corner
  8.   Ylr = Y + H - 1;
  9.   Wd  = W;  Ht  = H;  // Overall size
  10. }
  11.  
  12. void Rect::SetSize(int W, int H)
  13. // Sets the size of a rectangle and adjusts the lower-right 
  14. // coordinates 
  15. {
  16.   Wd  = W;  Ht = H; 
  17.   Xlr = Xul + W - 1;    // Adjust lower-right corner
  18.   Ylr = Yul + H - 1;
  19. }
  20.  
  21. void Rect::SetLocn(int Xl, int Yl)
  22. // Sets the top-left coordinates and adjust the lower-right 
  23. // coordinates also 
  24. {
  25.   Xul = Xl;  Yul = Yl; 
  26.   Xlr = Xl + Wd - 1;    
  27.   Ylr = Yl + Ht - 1;
  28. }
  29.  
  30. void Rect::ClipSize(int &W, int &H)
  31. // Checks width (W) and height (H) to ensure that they are not 
  32. // larger than the rectangle's dimensions. If they are, then 
  33. // they are adjusted to fit. Also, the sizes must be greater 
  34. // than zero.
  35. {
  36.   if (W > Wd) W = Wd;
  37.   if (W < 1)  W = 1;  
  38.   if (H > Ht) H = Ht;
  39.   if (H < 1)  H = 1;
  40. }
  41.  
  42. void Rect::ClipDim(int &X, int &Y, int &W, int &H)
  43. // Checks the incoming dimensions to ensure they are in range 
  44. // of the rectangle. If not, they are adjusted to fit.
  45. {
  46.   int Xl, Yl;
  47.   ClipSize(W, H);              // Make sure wd and ht are in range
  48.   if (X < Xul) X = Xul;        // Make sure upper left-hand
  49.   if (Y < Yul) Y = Yul;        // corner is in range
  50.   Xl = X + W - 1;              // Check right-hand coordinate
  51.   if (Xl > Xlr) X -= Xl - Xlr;
  52.   Yl = Y + H - 1;              // Check lower coordinate
  53.   if (Yl > Ylr) Y -= Yl - Ylr;
  54. }
  55.  
  56. int Rect::HzClip(int &X, int &Y, int &N)
  57. // Given a starting coordinate (X,Y) (relative to the rectangle),
  58. // and a length N, this function clips N to fit in the rectangle, 
  59. // assuming that the length runs horizontal. If X is negative, it 
  60. // is set to zero and the length adjusted accordingly. Y must be in
  61. // range. The function returns true if a non-null clip is found;
  62. // otherwise, false is returned. 
  63. {  
  64.   int Rval = False;
  65.   if ((Y >=0) && (Y < Ht)) { // Y must be in range
  66.      if (X < 0) {            // Clip to left
  67.         N += X;
  68.         X = 0;
  69.      }
  70.      if ((N > 0) && (X < Wd)) {
  71.         if ((X + N) > Wd) N = Wd - X; // Clip to right
  72.         Rval = True;
  73.      }
  74.   }
  75.   return Rval;
  76. }
  77.  
  78. int Rect::VtClip(int &X, int &Y, int &N)
  79. // Given a starting coordinate (X,Y) (relative to the rectangle),
  80. // and a length N, this function clips N to fit in the rectangle, 
  81. // assuming that the length runs vertical. If Y is negative, it 
  82. // is set to zero and the length adjusted accordingly. X must be in
  83. // range. This function returns true if a non-null clip is found; 
  84. // otherwise, false is returned.
  85. {
  86.   int Rval = False;
  87.   if ((X >=0) && (X < Wd)) { // X must be in range
  88.      if (Y < 0)  {           // Clip above
  89.         N += Y;
  90.         Y = 0;
  91.      }
  92.      if ((N > 0) && (Y < Ht))  {
  93.         if ((Y + N) > Ht)  N = Ht - Y; // Clip below
  94.         Rval = True;
  95.      }
  96.   }
  97.   return Rval;
  98. }
  99.  
  100. int Rect::Contains(int X, int Y)
  101. // Returns true if (X,Y) are contained in the rectangle
  102. {
  103.   return (X >= Xul) && (X <= Xlr) &&
  104.          (Y >= Yul) && (Y <= Ylr);
  105. }
  106.  
  107. int Rect::Touches(Rect *R)
  108. // Returns true if R overlaps Self. They overlap if their 
  109. // horizontal && vertical extents both cross. Note that as 
  110. // defined, a rectangle can't overlap itself. 
  111. {
  112.   if (R == this) return False; 
  113.   else return 
  114.       (Xul <= R->Xlr) && (Xlr >= R->Xul) && // Hz crossing
  115.       (Yul <= R->Ylr) && (Ylr >= R->Yul);   // Vt crossing
  116. }
  117.  
  118.  
  119.